<?php 
	$link_url = $_POST["link_url"];	
	//echo $link_url;

	$domainbase = str_replace("http://","",strtolower($link_url));
	$meta = get_meta_tags("http://".$domainbase);
	$page = file_get_contents("http://".$domainbase);
	if(!$page){
		echo "Cannot find: http://$domainbase";
		return;
	}
	$uid = $_POST["uid"];
	
?>
<table id='add_link'>
<tr>
  <td>
	<div class="header"> 
		<?php
			//title
			if($meta['title']!=null)
			{
				$title = $meta['title'];
			}
			else
			{
				$idx1 = stripos($page,"<title>")+7;
				$idx2 = stripos($page,"</title>",$idx1);
				$title = substr($page,$idx1,$idx2-$idx1);
				//echo 'getTitle():'.$title.'<br/>';
			}
			echo '<div id="link_title">'.$title.'</div>';
			echo '<font size="2" style="color:#666666;"><i><div id="link_url">http://'.$domainbase.'</div></i></font>';
		?>
	</div>
  </td>
</tr>
<tr>
  <td>
	<?php
		//image_src
		$pattern = "|<link[^>]+rel=\"image_src\"[^>]+href=\"([^\"]*)\"[^>]+>|i";
		preg_match_all($pattern, $page, $matches, PREG_SET_ORDER);

		if($matches[0][1] != null)
		{
			$image_src = $matches[0][1];
		}
		else
		{
			$image_src="images/no_image.gif";
		}
		$$image_src = htmlentities($image_src);
	?>
	<div class="desc">
		<img src="<?php echo $image_src ?>" alt="<?php echo $image_src_alt ?>" id="desc_pic"/> 
		<?php
			//description
			if($meta['description']!=null)
			{
				$desc = $meta['description'];
			}
			else
			{
				$desc = 'No Description available';
			}
			echo '<div id="link_desc"><textarea id="linkdesc_text" rows="5" cols="35">'.$desc.'</textarea></div>';
			$link_desc = htmlentities($desc);
		?>
	</div>
  </td>
</tr>
<tr>
  <td class='center'>
	<?php

		//list the groups so that the user can select in which group he wants to add the link
		$con = mysql_connect("localhost", "vatsav", "2T*vzcd");
		if (!$con)
		{
		  die('Could not connect: ' . mysql_error());
		}

		$db_selected = mysql_select_db("vatsav", $con);
		if (!$db_selected) {
			die ('Cant use db : ' . mysql_error());
		}
		$query = "select group_name,groupid from groups where uid=".$uid." order by groupid"; 

		$result = mysql_query($query);	
		//$num_rows = mysql_num_rows($result); //can be used later
		// Check result
		if (!$result) {
		   $message  = 'Invalid query: ' . mysql_error() . "<br/>";
		   echo $message;
		}
		else
		{
			echo "<div id='select_group'> Add to Group: <select id='grp' onchange='if(value==0) {document.getElementById(\"text_new_group\").disabled=false;}else{document.getElementById(\"text_new_group\").disabled=true;}'>";
			echo "<option value='0'> New Group </option>";
			while($row = mysql_fetch_array($result))
			{
				echo "<option value='".$row["groupid"]."'>".$row["group_name"]."</option>";
			}
			echo "</select> <input type='text' id='text_new_group' value='new group'/> </div>";
		}
		//list of tags assigned to the link
	?>
  </td>
</tr>

<tr>
  <td>
  	<div id="tag_container">
	Choose and/or add tags to the link:<br/>
	Enter a new tag name: <input type='text' id='taginput' onkeypress='addTag(event)'/> and press enter:
	<div id='newtag_container'>
		
	</div>
	<?php

		$query = "select tagid,tag from tags where uid=".$uid; 

		$result = mysql_query($query);	
		//$num_rows = mysql_num_rows($result); //can be used later
		// Check result
		if (!$result) {
		   $message  = 'Invalid query: ' . mysql_error() . "<br/>";
		   echo $message;
		}
		else
		{
			echo '<br/>';
			while($row = mysql_fetch_array($result))
			{
				echo ' <a class="oldtag" name="'.$row["tag"].'" onclick="javascript:toggleOldTag(this.childNodes[0].nodeValue);if(getAttribute(\'class\')==\'oldtag\') {setAttribute(\'class\',\'selected-oldtag\');} else  {setAttribute(\'class\',\'oldtag\');}">'.$row["tag"].'</a> ';
			}
		}

	?>
	</div>
  </td>
</tr>
</table>

<div class="button">
	<a onclick="javascript:sharelink(event)"> Share </a>
	<a onclick="javascript:recent(0,4)"> Cancel </a>
</div>